﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;

public partial class Controls_News_Listing : System.Web.UI.UserControl
{
    protected int CategoryId;    
    protected int PageIndex;
    protected int PageSize;

    protected void Page_Load(object sender, EventArgs e)
    {
        CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
        PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
        PageSize = Utils.GetIdFromRequestString(Utils.PageSize, SettingValues.PageSize_10);

        if (!IsPostBack)
        {
            ListPagingArticle();
        }
    }

    private void ListPagingArticle()
    {
        var totalRecords = 0;
        var lstArticle = ArticleController.Article_GetListOrderByPublishOn(CategoryId, PageIndex, PageSize, ref totalRecords);

        var categoryName = CategoryController.GetCategoryName(CategoryId);

        if (lstArticle.Count > 0)
        {
            rptNews.DataSource = lstArticle;
            rptNews.DataBind();

            ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, totalRecords, SettingValues.PageStep,
                UrlHelpers.ListArticleByCategoryPagination(CategoryId, UnicodeUtility.UnicodeToKoDauAndGach(categoryName).ToLower(), "{0}"));

            Page.Title = string.Format(".: Tin tức {0} :.", Constants.SITE_TITLE);
        }
    }

    protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
            var articleId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
            var title = ((HiddenField)e.Item.FindControl("hfTitle")).Value;

            var ltCategory = (Literal)e.Item.FindControl("ltCategory");
            var ltTitle = (Literal)e.Item.FindControl("ltTitle");
            var ltImage = (Literal)e.Item.FindControl("ltImage");

            var categoryName = CategoryController.GetCategoryName(categoryId);

            var cHref = UrlHelpers.ListArticleByCategory(categoryId, categoryName);
            var aHref = UrlHelpers.ArticleDetail(categoryName, ConvertUtility.ToInt32(articleId), title);

            var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=784", Constants.ThumbImage, articleId, (int)CategoryType.Article);

            ltCategory.Text = string.Format("<a href=\"{0}\" class=\"black under\">{1}</a>", cHref, categoryName);
            ltTitle.Text = string.Format("<a href=\"{0}\">{1}</a>", aHref, title);
            ltImage.Text = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"\" class=\"left\" /></a>", aHref, imgHref);
        }
    }
}